
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Namespace;



public class BuyItemsServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	HttpSession session = null;
	Namespace ns = Namespace.getNamespace("http://www.cs.au.dk/dWebTek/2011");

	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,
	IOException {
		
		session = req.getSession();
		session.setMaxInactiveInterval(200);
		List<String> itemID = (List<String>) session.getAttribute("itemIDs");
		String customerID = (String) session.getAttribute("authorizedID");
		List<String> saleAmount = (List<String>) session.getAttribute("amount");
		
		for (int i = 0; i < itemID.size(); i++){
			ItemHandler.sellItem(itemID.get(i), customerID, saleAmount.get(i));
		}

		
		// Redirecting the client to the frontpage and clear shopping cart
		session.setAttribute("amount",null);
		session.setAttribute("products",null);
		session.setAttribute("itemIDs",null);
		
		res.sendRedirect("index.jsp");
		return;
	}
}

